package com.prs.crm.dao.crm.impl;

import java.util.Date;
import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.LiaisonDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.Liaison;
import com.prs.crm.domain.hr.Leave;

public class LiaisonDaoImpl extends BaseDaoImpl<Liaison> implements LiaisonDao{

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Liaison> getAllLiaison() {
		// TODO Auto-generated method stub
		return (Collection<Liaison>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Liaison.class);
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Liaison> getLiaison(final Customer customer) {
		// TODO Auto-generated method stub
		return (Collection<Liaison>)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Liaison.class);
				criteria.add(Restrictions.eq("customer", customer));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public boolean isExistLiaison(final Date startTime, final Date endTime,final Customer customer) {
		// TODO Auto-generated method stub
		return (Boolean)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Boolean flag = false;
				Criteria criteria = arg0.createCriteria(Liaison.class);
				criteria.createAlias("customer", "c");
				criteria.add(Restrictions.ge("recordTime", startTime));
				criteria.add(Restrictions.le("recordTime", endTime));
				if(criteria.list().size() != 0) {
					flag = true;
				}
				return flag;
			}
			
		});
	}

	@Override
	public Collection<Liaison> getLiaisonByIds(Collection<Integer> ids) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Liaison.class);
		criteria.add(Restrictions.in("id", ids));
		return criteria.list();
		
	}

}
